﻿using AIClient.Players.Modules;
using AIClient.Utils;
using NFX.Erlang;
using System.Collections.Generic;

namespace AIClient.Players
{
    public class Player
    {
        public readonly  long    PlayerId;
        public readonly  Gate    Gate;
        public readonly  Card    Card;
        private readonly ErlAtom _gatewayName;

        public Player(long playerId)
        {
            PlayerId     = playerId;
            Gate         = new Gate(this);
            Card         = new Card(this);
            _gatewayName = new ErlAtom($"gateway_{PlayerId}");
        }

        public void StartThinking()
        {
        }


        public void Send(ErlBinary binary)
        {
            /* ErlTuple的通用构造函数是通过反射来实现的,效率不高
                               所以对于像玩家消息发送,这种经常使用到的功能,最好手动构造参数列表
                            */
            var manager = Manager.Instance;
            manager.MessageHub.Send(_gatewayName
                                  , new ErlTuple(new List<IErlObject> {new ErlAtom("from_player"), binary}
                                               , false));
        }

        public void Dispatch(byte[] binary)
        {
            Gate.Dispatch(binary);
        }

        public void LogDebug(string logMessage)
        {
            Logger.LogDebug($"Player {PlayerId} : {logMessage}");
        }

        public void LogWarning(string logMessage)
        {
            Logger.LogWarning($"Player {PlayerId} : {logMessage}");
        }

        public void LogError(string logMessage)
        {
            Logger.LogError($"Player {PlayerId} : {logMessage}");
        }

        public void Stop()
        {
            Gate.Close();
        }
    }
}